<?php


namespace app\controller\api;


use app\model\AdminCheckInRecord;
use app\model\AdminStaff;
use app\util\ReturnCode;
use think\Response;
use think\facade\Validate;

class SignIn extends Base
{
    /**
     * @func submit
     * @method POST
     * @desc 签到
     * @return Response
     */
    public function submit(): Response {
        $validate = Validate::rule([
                'uuid|用户'  => 'require',
                'school_id|学校' => 'require',
                'address|当前定位' => 'require'
            ]);
        if (!$validate->check(input('post.'))) {
            return $this->buildFailed(ReturnCode::PARAM_INVALID, $validate->getError());
        }
        $staff = (new AdminStaff())->where('school_id', input('post.school_id'))
            ->where('uuid', input('post.uuid'))
            ->where('is_delete', 0)->findOrEmpty();
        if ($staff->isEmpty()) {
            return $this->buildFailed(ReturnCode::RECORD_NOT_FOUND, '账号不存在');
        }
        $log = (new AdminCheckInRecord())
            ->where('school_id', input('post.school_id'))
            ->where('staff_id', $staff->id)
            ->whereDay('create_time')
            ->findOrEmpty();
        if (!$log->isEmpty()) {
            return $this->buildFailed(ReturnCode::DATA_EXISTS, '今日已签到，明天再来哦~');
        }
        $pre_times = (new AdminCheckInRecord())
            ->where('school_id', input('post.school_id'))
            ->where('staff_id', $staff->id)
            ->whereDay('create_time', 'yesterday')->value('continuous_times');
        AdminCheckInRecord::create([
            'school_id' => input('post.school_id'),
            'staff_id' => $staff->id,
            'address' => input('post.address'),
            'continuous_times' => $pre_times ? $pre_times+1 : 1
        ]);
        return $this->buildSuccess();
    }

    /**
     * @func record
     * @method GET
     * @desc 获取签到记录
     * @return Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Time: 2021/12/7 18:49
     */
    public function record(): Response {
        $uuid = input('get.uuid');
        $validate = Validate::rule([
            'uuid|用户'  => 'require',
            'school_id|学校' => 'require',
        ]);
        if (!$validate->check(input('get.'))) {
            return $this->buildFailed(ReturnCode::PARAM_INVALID, $validate->getError());
        }
        $staff = AdminStaff::where('uuid', $uuid)
            ->where('is_delete', 0)
            ->where('status', AdminStaff::AUDIT_PASS)->find();
        if (!$staff) {
            return $this->buildFailed(ReturnCode::RECORD_NOT_FOUND, '账户未找到');
        }
        $recordList = AdminCheckInRecord::where('school_id', input('get.school_id'))
            ->where('staff_id', $staff->id)
            ->field("address,create_time,'签到成功' as result")
            ->page($this->page, $this->pageSize)
            ->order('id desc')
            ->select()->toArray();
        return $this->buildSuccess($recordList);
    }
}